<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="仓库来源" prop="warehouseId">
        <el-input
          v-model="queryParams.warehouseId"
          placeholder="请输入仓库来源"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="父级id" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入父级id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入仓库名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="全名" prop="fullName">
        <el-input
          v-model="queryParams.fullName"
          placeholder="请输入全名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="leader">
        <el-input
          v-model="queryParams.leader"
          placeholder="请输入负责人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择仓库状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="仓库类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择仓库类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="级别名称(0仓库，1仓位，2货架，3货位)" prop="levelName">
        <el-select v-model="queryParams.levelName" placeholder="请选择级别名称(0仓库，1仓位，2货架，3货位)" clearable size="small">
          <el-option
            v-for="dict in levelNameOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建人" prop="createUser">
        <el-input
          v-model="queryParams.createUser"
          placeholder="请输入创建人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改人" prop="updateUser">
        <el-input
          v-model="queryParams.updateUser"
          placeholder="请输入修改人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否删除  0否  1是" prop="isDel">
        <el-input
          v-model="queryParams.isDel"
          placeholder="请输入是否删除  0否  1是"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库管理员" prop="godownKeeper">
        <el-input
          v-model="queryParams.godownKeeper"
          placeholder="请输入仓库管理员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入仓库地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库属性" prop="properties">
        <el-input
          v-model="queryParams.properties"
          placeholder="请输入仓库属性"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="允许负仓库" prop="allowNegative">
        <el-input
          v-model="queryParams.allowNegative"
          placeholder="请输入允许负仓库"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否MPS/MRP可用量" prop="mesMrp">
        <el-input
          v-model="queryParams.mesMrp"
          placeholder="请输入是否MPS/MRP可用量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否将进行仓位管理" prop="positionManagement">
        <el-input
          v-model="queryParams.positionManagement"
          placeholder="请输入是否将进行仓位管理"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓位组" prop="positionGroup">
        <el-input
          v-model="queryParams.positionGroup"
          placeholder="请输入仓位组"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参与核算" prop="participateAccounting">
        <el-input
          v-model="queryParams.participateAccounting"
          placeholder="请输入参与核算"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参与物料出库规则校验" prop="deliveryVerification">
        <el-input
          v-model="queryParams.deliveryVerification"
          placeholder="请输入参与物料出库规则校验"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库编号" prop="erpCode">
        <el-input
          v-model="queryParams.erpCode"
          placeholder="请输入仓库编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库来源" prop="sourceWarehouse">
        <el-select v-model="queryParams.sourceWarehouse" placeholder="请选择仓库来源" clearable size="small">
          <el-option
            v-for="dict in sourceWarehouseOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:warehouse:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:warehouse:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:warehouse:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:warehouse:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table 
    v-loading="loading" 
    :data="warehouseList" 
    @selection-change="handleSelectionChange"
    row-key="warehouseId"
     :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="仓库id" align="center" prop="warehouseId" />
      <el-table-column label="父级id" align="center" prop="parentId" /> -->
      <el-table-column label="仓库名称" align="center" prop="warehouseName" width="200px" />
      <el-table-column label="全名" align="center" prop="fullName" />
      <el-table-column label="负责人" align="center" prop="leader" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="仓库状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="仓库类型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="级别名称" align="center" prop="levelName" :formatter="levelNameFormat" />
      <!-- <el-table-column label="创建人" align="center" prop="createUser" />
      <el-table-column label="修改人" align="center" prop="updateUser" /> -->
     
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="仓库管理员" align="center" prop="godownKeeper" />
      <el-table-column label="仓库地址" align="center" prop="address" />
      <el-table-column label="仓库属性" align="center" prop="properties" :formatter="propertiesFormat" />
      <el-table-column label="允许负仓库" align="center" prop="allowNegative" />
      <el-table-column label="是否MPS/MRP可用量" align="center" prop="mesMrp" />
      <el-table-column label="是否将进行仓位管理" align="center" prop="positionManagement" />
      <el-table-column label="仓位组" align="center" prop="positionGroup" />
      <el-table-column label="参与核算" align="center" prop="participateAccounting" />
      <el-table-column label="参与物料出库规则校验" align="center" prop="deliveryVerification" />
      <el-table-column label="仓库编号" align="center" prop="erpCode" />
      <el-table-column label="仓库来源" align="center" prop="sourceWarehouse" :formatter="sourceWarehouseFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:warehouse:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:warehouse:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="上级仓库" prop="parentId">
           <treeselect v-model="form.parentId" :options="warehousetOptions" :normalizer="normalizer" placeholder="选择上级仓库" />
          
       </el-form-item>
        <el-form-item label="仓库名称" prop="warehouseName">
          <el-input v-model="form.warehouseName" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="全名" prop="fullName">
          <el-input v-model="form.fullName" placeholder="请输入全名" />
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="form.leader" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="仓库状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择仓库状态">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="仓库类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择仓库类型">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="级别名称(0仓库，1仓位，2货架，3货位)" prop="levelName">
          <el-select v-model="form.levelName" placeholder="请选择级别名称(0仓库，1仓位，2货架，3货位)">
            <el-option
              v-for="dict in levelNameOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="仓库管理员" prop="godownKeeper">
          <el-input v-model="form.godownKeeper" placeholder="请输入仓库管理员" />
        </el-form-item>
        <el-form-item label="仓库地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入仓库地址" />
        </el-form-item>
        <el-form-item label="仓库属性" prop="properties">
          <el-select v-model="form.properties" placeholder="请选择仓库属性">
            <el-option
              v-for="dict in propertiesOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="允许负仓库" prop="allowNegative">
           <el-select v-model="form.allowNegative" placeholder="请输入允许负仓库">
    <el-option
      v-for="item1 in allowNegativeOptions"
      :key="item1.value"
      :label="item1.label"
      :value="item1.value">
    </el-option>
  </el-select>
        </el-form-item>
        <el-form-item label="是否MPS/MRP可用量" prop="mesMrp">
          <el-input v-model="form.mesMrp" placeholder="请输入是否MPS/MRP可用量" />
        </el-form-item>
        <el-form-item label="是否将进行仓位管理" prop="positionManagement">
          <el-input v-model="form.positionManagement" placeholder="请输入是否将进行仓位管理" />
        </el-form-item>
        <el-form-item label="仓位组" prop="positionGroup">
          <el-input v-model="form.positionGroup" placeholder="请输入仓位组" />
        </el-form-item>
        <el-form-item label="参与核算" prop="participateAccounting">
          <el-input v-model="form.participateAccounting" placeholder="请输入参与核算" />
        </el-form-item>
        <el-form-item label="参与物料出库规则校验" prop="deliveryVerification">
          <el-input v-model="form.deliveryVerification" placeholder="请输入参与物料出库规则校验" />
        </el-form-item>
        <el-form-item label="仓库编号" prop="erpCode">
          <el-input v-model="form.erpCode" placeholder="请输入仓库编号" />
        </el-form-item>
        <el-form-item label="仓库来源" prop="sourceWarehouse">
          <el-select v-model="form.sourceWarehouse" placeholder="请选择仓库来源">
            <el-option
              v-for="dict in sourceWarehouseOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWarehouse, getWarehouse, delWarehouse, addWarehouse, updateWarehouse, exportWarehouse,listDeptExcludeChild } from "@/api/base/warehouse";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  warehouseName: "Warehouse",
  components: {Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
          // 仓库树选项
      warehousetOptions: [],
      // 仓库表格数据
      warehouseList: [],
            // 仓库属性字典
      propertiesOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 仓库状态字典
      statusOptions: [],
      // 仓库类型字典
      typeOptions: [],
      // 级别名称(0仓库，1仓位，2货架，3货位)字典
      levelNameOptions: [],
      // 仓库来源字典
      sourceWarehouseOptions: [],
      //仓库是否允许负库存
      allowNegativeOptions:[{
          value: '是',
          label: '是'
        }, {
          value: '否',
          label: '否'
        },
      ],
  
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseId: undefined,
        parentId: undefined,
        warehouseName: undefined,
        fullName: undefined,
        leader: undefined,
        phone: undefined,
        status: undefined,
        type: undefined,
        levelName: undefined,
        createUser: undefined,
        updateUser: undefined,
        isDel: undefined,
        godownKeeper: undefined,
        address: undefined,
        properties: undefined,
        allowNegative: undefined,
        mesMrp: undefined,
        positionManagement: undefined,
        positionGroup: undefined,
        participateAccounting: undefined,
        deliveryVerification: undefined,
        erpCode: undefined,
        sourceWarehouse: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("base_warehouse_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("base_warehouse_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("base_warehouse_level_name").then(response => {
      this.levelNameOptions = response.data;
    });
    this.getDicts("base_warehouse_source_warehouse").then(response => {
      this.sourceWarehouseOptions = response.data;
    });
    this.getDicts("base_warehouse_properties").then(response => {
      this.propertiesOptions = response.data;
    });
  },
  methods: {
       /** 查询部门列表 */
    getList() {
      this.loading = true;
      listWarehouse(this.queryParams).then(response => {
        this.warehouseList = this.handleTree(response.data, "warehouseId");
       console.log(this.warehouseList )
      
        this.loading = false;
      });
    },
        /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.warehouseId,
        label: node.warehouseName,
        children: node.children
      };
    },
    // 仓库状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 仓库类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 级别名称(0仓库，1仓位，2货架，3货位)字典翻译
    levelNameFormat(row, column) {
      return this.selectDictLabel(this.levelNameOptions, row.levelName);
    },
        // 仓库属性字典翻译
    propertiesFormat(row, column) {
      return this.selectDictLabel(this.propertiesOptions, row.properties);
    },
    // 仓库来源字典翻译
    sourceWarehouseFormat(row, column) {
      return this.selectDictLabel(this.sourceWarehouseOptions, row.sourceWarehouse);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        warehouseId: undefined,
        parentId: undefined,
        warehouseName: undefined,
        fullName: undefined,
        leader: undefined,
        phone: undefined,
        status: undefined,
        type: undefined,
        levelName: undefined,
        createTime: undefined,
        createUser: undefined,
        updateTime: undefined,
        updateUser: undefined,
        isDel: undefined,
        remark: undefined,
        godownKeeper: undefined,
        address: undefined,
        properties: undefined,
        allowNegative: undefined,
        mesMrp: undefined,
        positionManagement: undefined,
        positionGroup: undefined,
        participateAccounting: undefined,
        deliveryVerification: undefined,
        erpCode: undefined,
        sourceWarehouse: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.warehouseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
     handleAdd(row) {
      this.reset();
      if (row != undefined) {
        this.form.parentId = row.warehouse_id;
      }
      this.open = true;
      this.title = "添加部门";
      listWarehouse().then(response => {
          this.warehousetOptions = this.handleTree(response.data, "warehouseId");
          console.log(this.warehousetOptions)
      });
    },
    /** 修改按钮操作 */

     handleUpdate(row) {
      this.reset();
      getWarehouse(row.warehouseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改部门";
      });
      listDeptExcludeChild(row.warehouseId).then(response => {
	        this.warehousetOptions = this.handleTree(response.data, "warehouseId");
      });
    },
    // handleUpdate(row) {
    //   this.reset();
    //   const warehouseId = row.warehouseId || this.ids
    //   getWarehouse(warehouseId).then(response => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "修改仓库";
    //   });
    // },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.warehouseId != null) {
            updateWarehouse(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarehouse(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const warehouseIds = row.warehouseId || this.ids;
      this.$confirm('是否确认删除仓库编号为"' + warehouseIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWarehouse(warehouseIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有仓库数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportWarehouse(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
